<?php 
session_start();
require_once('include/connect.inc.php'); 
$_SESSION['set_agree'] = $_GET[set_id];
if($_SESSION['sess_id'] <> session_id()){
?>
 <SCRIPT LANGUAGE="JavaScript">
			alert("กรุณาเข้าสู่ระบบให้ถูกต้องตามขั้นตอน...!");
			location="login.php";
</script>
<?php }?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Model Test Program</title>
<link rel="stylesheet" type="text/css" href="template/template_style_wood.css">
<script language="javascript"> 
		function fncSubmit()
		{
        	if(document.form2.select.value == "0"){
				alert('กรุณาเลือกวิชา');
				document.form2.select.focus();       
				return false;
			}  
			document.form2.submit();
	}
</script>
<script language=JavaScript type=text/javascript>
<!-- 
function CheckAll() {
for (var i = 0; i < document.messageList.elements.length; i++) {
if(document.messageList.elements[i].type == 'checkbox'){
document.messageList.elements[i].checked = !(document.messageList.elements[i].checked);
}
}
}
//-->
</script>
</head>

<body>
<div id="menu"><div><img src="../images/icon-m.png" /></div>
<div id="menulinks">
		<a  href="person_tea.php">ข้อมูลส่วนตัว</a>
		<a href="tea_edit.php">เปลี่ยนรหัสผ่านใหม่</a>
        
        <a  href="exam/show_subject.php">จัดการรายวิชา</a>
		<a href="teacher_test.php">จัดการข้อสอบ</a>
        <a href="teacher_tests.php">จัดการชุดข้อสอบ</a>
        <a  class="active" href="agree_set.php">กำหนดสิทธิ์</a>
		<a href="exam/tea_chack.php">ตรวจสอบคะแนน</a>
        <a href="news/manage_news.php">ประกาศข่าวสาร</a>
        <a href="problem/send_problem.php">แสดงความคิดเห็น</a>
<a  href="problem/teacher_message.php">กล่องข้อความ</a>

        <a href="logout.php">ออกจากระบบ</a>
		</div></div>
 <div id="p3">    
<div id="header"></div>
<div id="page"><div id="p1">คุณ<?php echo $_SESSION['teacher_name'];?><hr />
<form id="form1" name="form1" method="get" action="agree_student.php" onSubmit="JavaScript:return fncSubmit();">
	<input type="hidden" name="subject_select" id="subject_select"></input>
    <input type="hidden" name="set_id" id="set_id" value="<?php echo $_SESSION['set_agree'];?>" /></input>
<table width="344" border="0">
<tr>
	<td width="170">เลือกวิชาที่ลงทะเบียน</td>
	<td width="164">
		<select name="select" id="select">
			<option selected value="0">===เลือกวิชา===</option>
				<?php  
					$query3= query("SELECT * FROM tb_subject");
						while  ($rs = fetch_array($query3)){
							$subject_id=$rs['subject_id'];
							$subject_name=$rs['subject_name'];
				?>
            <option  value="<?php echo $subject_id;?>"><?php echo $subject_name;?></option>	
			<?php }?>
         </select> 
     </td>
	</tr>
    	<tr>
            <td></td>
            <td>
            <input type="submit" name="submit_subject" id="submit_subject" value="ตกลง" />
            <input type="submit" name="all_choose" id="all_choose" value="เลือกทั้งหมด" />
            </td>
          </tr>
	</table>
</form>

<?php if ($submit_subject=='ตกลง'){  ?>
<form id="messageList" name="messageList" method="get" action="agree_add.php">
<input type="hidden" name="set_id" id="set_id" value="<?php echo $_SESSION['set_agree'];?>" />
<?php 
	$query3= query("SELECT * FROM tb_subject where subject_id='$select' ");
		while  ($rs = fetch_array($query3)){
			$subject_name=$rs['subject_name'];
?>
ให้สิทธิ์ผู้ทำข้อสอบเฉพาะวิชา <?php echo "$subject_name";?>
<?php }?>
<table border="1"  id="table"  summary="2007 Major IT Companies Profit">
      <tr>
        <th align="center"><input type="checkbox" name="Button" id="checkbox" value="Select All" onClick=CheckAll(); ></th>
        <th align="center">ลำดับ</th>
        <th align="center">รหัสประจำตัว</th>
        <th align="center">ชื่อ - สกุล</th>
         <th align="center">คณะ</th>
        <th align="center">สาขาวิชา</th>
        <th align="center">ชั้นปี</th>
         <th align="center">ถอนสิทธิ์</th>
        </tr>
<?php
	$query = query("SELECT * FROM tb_register
		Inner Join tb_student ON tb_student.r_student_id = tb_register.r_student_id
		Inner Join tb_fac ON tb_fac.fac_id = tb_student.fac_id
		Inner Join tb_major ON tb_major.major_id = tb_student.major_id
		Inner Join tb_subject ON tb_subject.subject_id = tb_register.subject_id where tb_register.subject_id='$select'");
		$count=1;
			while ($rs = fetch_array($query)) {
				$r_student_id 			= $rs['r_student_id'];
				$student_password = $rs['student_password'];
				$student_fname 		= $rs['student_fname'];
				$student_lname 		= $rs['student_lname'];
				$fac_name				= $rs['fac_name'];
				$major_name 			= $rs['major_name'];
				$year 						= $rs['year'];
				$student_email 		= $rs['student_email'];
				$student_phone 		= $rs['student_phone'];
		?>
        <tr>
        	<td align="center">
				<?php 
					$query1= query("SELECT * FROM tb_agree where (set_id='$set_id') and (r_student_id='$r_student_id')");
						if(num_rows($query1)>0){ 
						echo " ";
					}else{
				?>
        <input type="checkbox" name="r_student_id[]" id="checkbox" value="<?php echo $r_student_id; ?>">
<?php }?>        </td>
        <td align="center"><?php echo $count;echo ".";?></td>
        <td><?php echo $r_student_id;?></td>
        <td><?php echo $student_fname; echo " "; echo $student_lname; ?></td>
      	<td ><?php echo $fac_name;?></td>
      	<td ><?php echo $major_name;?></td>
      	<td align="center" ><?php echo $year;?></td>
		<td align="center">
<?php
$query0= query("SELECT * FROM tb_agree where (set_id='$set_id') and (r_student_id='$r_student_id')");
	if(num_rows($query0)==0){ 
	echo "ยังไม่ได้ให้สิทธิ์";
	}else{
?>
<a href="agree_del.php?del_id=<?php echo $r_student_id;?>&set_id=<?php echo $set_id?>" onClick="return confirm('ยืนยันการยกเลิกสิทธิ์การสอบ')">ลงมือ</a>
<?php }?>      </td>
      </tr>
       <?php $count++; } ?>
</table>
     <input type="submit" name="agree" id="agree" value="ให้สิทธิ์ทำข้อสอบ"  onclick="return confirm('ยืนยันการให้สิทธิ์')">
     <input type="reset" name="Reset" id="button" value="ยกเลิก" />
</form>
<?php }?>

<?php if ($all_choose=='เลือกทั้งหมด'){  ?>
<form name="messageList" id="messageList" method="get" action="agree_add.php">
<input type="hidden" name="set_id" id="set_id" value="<?php echo $_SESSION['set_agree'];?>" />
    	ให้สิทธิ์ผู้ทำข้อสอบผู้ทำข้อสอบทั้งหมด
	  <table border="0"  id="table"  summary="2007 Major IT Companies Profit">
      <tr>
        <th align="center"><input type="checkbox" name="Button" id="checkbox" value="Select All" onClick=CheckAll(); ></th>
        <th align="center">ลำดับ</th>
        <th align="center">รหัสประจำตัว</th>
        <th align="center">ชื่อ - สกุล</th>
         <th align="center">คณะ</th>
        <th align="center">สาขาวิชา</th>
        <th align="center">ชั้นปี</th>
        <th align="center">ถอนสิทธิ์</th>
        </tr>
<?php
	$query = query("SELECT * FROM tb_register
		Inner Join tb_student ON tb_student.r_student_id = tb_register.r_student_id
		Inner Join tb_fac ON tb_fac.fac_id = tb_student.fac_id
		Inner Join tb_major ON tb_major.major_id = tb_student.major_id
		Inner Join tb_subject ON tb_subject.subject_id = tb_register.subject_id");
			$count=1;
			while ($rs = fetch_array($query)) {
				$r_student_id 			= $rs['r_student_id'];
				$student_password = $rs['student_password'];
				$student_fname 		= $rs['student_fname'];
				$student_lname 		= $rs['student_lname'];
				$fac_name				= $rs['fac_name'];
				$major_name 			= $rs['major_name'];
				$year 						= $rs['year'];
				$student_email 		= $rs['student_email'];
				$student_phone 		= $rs['student_phone'];
		?>
      <tr>
        <td align="center">
<?php 
$query1= query("SELECT * FROM tb_agree where (set_id='$set_id') and (r_student_id='$r_student_id')");
	if(num_rows($query1)>0){ 
	echo " ";
	}else{
?>
        <input type="checkbox" name="r_student_id[]" id="checkbox" value="<?php echo $r_student_id; ?>">
<?php }?>        </td>
        <td align="center"><?php echo $count;echo ".";?></td>
        <td><?php echo $r_student_id;?></td>
        <td><?php echo $student_fname; echo " "; echo $student_lname; ?></td>
      	<td ><?php echo $fac_name;?></td>
      	<td ><?php echo $major_name;?></td>
      	<td align="center" ><?php echo $year;?></td>
		<td align="center" >
<?php
$query0= query("SELECT * FROM tb_agree where (set_id='$set_id') and (r_student_id='$r_student_id')");
	if(num_rows($query0)==0){ 
	echo "ยังไม่ได้ให้สิทธิ์";
	}else{
?>
<a href="agree_del.php?del_id=<?php echo $r_student_id;?>&set_id=<? echo $set_id?>" onClick="return confirm('ยืนยันการยกเลิกสิทธิ์การสอบ')">ลงมือ</a>
<?php }?>      </td>
      </tr>
       <?php $count++; } ?>
    </table>
     <input type="submit" name="agree" id="agree" value="ให้สิทธิ์ทำข้อสอบ"  onclick="return confirm('ยืนยันการให้สิทธิ์')">
     <input type="reset" name="Reset" id="button" value="ยกเลิก" />
</form>
<?php }?>
    </div>
    </div>
<div id="footer"></div>
</div>
</body>
</html>
